#include "StdAfx.h"
#include "xhImageAnaly.h"

xhImageAnaly::xhImageAnaly(void)
{
	m_pImgBit = NULL;
}

xhImageAnaly::~xhImageAnaly(void)
{
	m_pImgBit = NULL;
}

//init
int xhImageAnaly::Initialize()
{
	m_pImgBit = NULL;

	return 0;
}
//set parent hwnd
int xhImageAnaly::SetHwnd(HWND hWnd)
{
	m_xDisp.SetParent( hWnd );
	m_xDisp.SetDisplayPalette();

	return 0;
}


//standard iamge
int xhImageAnaly::LoadStdFile(char *pFileName)
{
	int nRet = DD_ERR_FAILURE;

	DD_AUTO_LOCK xLock(&m_Lock);
	m_pImgBit = NULL;

	if( NULL == pFileName || strlen(pFileName) < 4 )
	{
		return nRet;
	}

	//xkImgCheck xCheck;
	DWORD dwBitPerSap = 16;
	//dwBitPerSap = xCheck.CheckFileBitPerSap( pFileName );

	DWORD dwImgDefine = eXKImage_Define_Spc;

	switch( dwBitPerSap )  
	{
	case 1:
	case 2:
	case 3:
	case 8:
		{
			//m_pImgBit = CreateImgBit8();

			dwImgDefine = eXKImage_Define_Spc;
		}
		break;
	case 10:
	case 12:
	case 14:
	case 16:
		{
			m_pImgBit = new xkImgAnalyBit16;
			
			dwImgDefine = eXKImage_Define_Img16;
		}
		break;
	case 32:
		{
		}
		break;
	default:
		break;
	} 

	if( NULL != m_pImgBit )
	{
		nRet = m_pImgBit->LoadStdFile(pFileName);
		if( DD_ERR_SUCCESS != nRet )
		{
			return nRet;
		}
	}
	else
	{
		nRet = -1;
	}

	return nRet;
}
//
int xhImageAnaly::SetStdImgBuffer(zkImgBitInfo *pImgInfo)
{
	int nRet = DD_ERR_FAILURE;

	if( NULL == pImgInfo )
	{
		return nRet;
	}

	DD_AUTO_LOCK xLock(&m_Lock);
	m_pImgBit = NULL;
	
	switch( pImgInfo->dwImgDefine ) 
	{
	case eXKImage_Define_Spc:
		{
			m_pImgBit = &m_xImgAnalyBit8;
		}
		break;
	case eXKImage_Define_Img16:
		{
			m_pImgBit = &m_xImgAnalyBit16;
		}
		break;
	case eXKImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}

	do 
	{
		if( NULL == m_pImgBit )
		{
			break;
		}

		nRet = m_pImgBit->SetStdImgBuffer(pImgInfo);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}
		
		DefaultDisplayPalette(pImgInfo);

		nRet = LoadDataToDisp();
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}		

	} while (false);

	return nRet;
}
//
int xhImageAnaly::StdImgOperate(DWORD dwAnalyType,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);
	if( NULL == m_pImgBit )
	{
		return DD_ERR_FAILURE;
	}

	int nRet = DD_ERR_FAILURE;
	do 
	{
		nRet = m_pImgBit->StdImgOperate(dwAnalyType,pVoid);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		nRet = LoadDataToDisp();
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	} while (FALSE);

	return nRet;
}

void xhImageAnaly::DefaultDisplayPalette(zkImgBitInfo *pImgInfo)
{
	int nDark   = 0;
	int nBright = 255;
	int nBpp    = 8;

	switch( pImgInfo->dwImgDefine ) 
	{
	case eXKImage_Define_Spc:
		{

		}
		break;
	case eXKImage_Define_Img16:
		{
			nBpp = ( (zkImg16BitInfo *)pImgInfo->pImageHeader )->nBitPerSample;
			nBright = (1<<nBpp) - 1;
		}
		break;
	case eXKImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}

	m_xDisp.SetDisplayPalette(nDark,nBright,nBpp);
}

int xhImageAnaly::LoadDataToDisp()
{
	int nRet = -1;

	zkImgBitInfo *pBitInfo = NULL;
	nRet = m_pImgBit->GetImgBuffer(&pBitInfo);
	if( DD_ERR_SUCCESS != nRet )
	{
		return nRet;
	}

	nRet = m_xDisp.SetDisplayData(pBitInfo);

	return nRet;
}


int	xhImageAnaly::Draw(void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	return m_xDisp.SetDisplayDst(pVoid);
}

int xhImageAnaly::ImageCtrl(DWORD dwType,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Lock);

	int nRet = 0;

	switch( dwType )
	{
	case ImgAnaly_BinaryImage:
		{
			if( NULL == pVoid ) 
			{
				nRet = DD_ERR_PARAMETER;
				break;
			}

			int *pTemp = (int *)pVoid; 

			int nLeft  = *(pTemp);
			int nRight = 65535;
			
			int nBpp = 16;
			nRet = m_xDisp.SetDisplayPalette(nLeft,nRight,nBpp);

			if( NULL != m_pImgBit )
			{
				nRet = LoadDataToDisp();
			}
		}
		break;
	default:
		break;
	}

	return 0;
}

//To be Checked image
int xhImageAnaly::SetChkImgBuffer(zkImgBitInfo *pImgInfo)
{
	DD_AUTO_LOCK xLock(&m_Lock);
	if( NULL == m_pImgBit )
	{
		return DD_ERR_FAILURE;
	}

	int nRet = DD_ERR_FAILURE;

	do 
	{
		nRet = m_pImgBit->SetChkImgBuffer(pImgInfo);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	} while (FALSE);

	return nRet;
}
//
int xhImageAnaly::ChkImgOperate(DWORD dwAnalyType,void *pVoid )
{
	DD_AUTO_LOCK xLock(&m_Lock);
	if( NULL == m_pImgBit )
	{
		return DD_ERR_FAILURE;
	}

	int nRet = DD_ERR_FAILURE;
	do 
	{
		nRet = m_pImgBit->ChkImgOperate(dwAnalyType,pVoid);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	} while (FALSE);

	return nRet;
}


int xhImageAnaly::ImgAnaly()
{
	DD_AUTO_LOCK xLock(&m_Lock);
	if( NULL == m_pImgBit )
	{
		return DD_ERR_FAILURE;
	}

	return m_pImgBit->ImgAnaly();
}
